package com.letwal.cmcvnd.assetdetail.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.user.RevDataGetMyTransactions
import com.letwal.cmcvnd.extend.toEllipsisWalletAddress
import com.letwal.cmcvnd.extend.toFinanceString

class AssetDetailAdapter(val dataList: List<RevDataGetMyTransactions>) :
    RecyclerView.Adapter<AssetDetailAdapter.ViewHolder>() {

    //点击事件
    private var mItemClickListener: (RevDataGetMyTransactions) -> Unit = {}

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var rlAdapterAssetDetailContent: RelativeLayout =
            view.findViewById(R.id.rlAdapterAssetDetailContent)
        var imageAdapterAssetDetail: ImageView =
            view.findViewById(R.id.imageAdapterAssetDetail)
        var textAdapterAssetDetailName: TextView =
            view.findViewById(R.id.textAdapterAssetDetailName)
        var textAdapterAssetDetailNameRef: TextView =
            view.findViewById(R.id.textAdapterAssetDetailNameRef)
        var textAdapterAssetDetailAmount: TextView =
            view.findViewById(R.id.textAdapterAssetDetailAmount)
        var textAdapterAssetDetailStatus: TextView =
            view.findViewById(R.id.textAdapterAssetDetailStatus)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_asset_detail, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: RevDataGetMyTransactions = dataList[position]
        holder.textAdapterAssetDetailName.text = data.address.toEllipsisWalletAddress()
        holder.textAdapterAssetDetailNameRef.text = data.createTime

//        if([[item.status lowercaseString] containsString:@"fail"]){
//            self.statusLabel.text = @"Failure";
//            self.statusLabel.backgroundColor = [UIColor hexColor:@"FF575C"];
//
//        } else if([[item.status lowercaseString] containsString:@"succe"]){
//            self.statusLabel.text = @"Successful";
//            self.statusLabel.backgroundColor = [UIColor hexColor:@"65CA79"];
//
//        } else if([[item.status lowercaseString] containsString:@"process"]){
//            self.statusLabel.text = @"Processing";
//            self.statusLabel.backgroundColor = kThemeColor;//[UIColor hexColor:@"65CA79"];
//
//        } else if([[item.status lowercaseString] containsString:@"create"]){
//            self.statusLabel.text = @"Processing";
//            self.statusLabel.backgroundColor = kThemeColor;//[UIColor hexColor:@"65CA79"];
//        }

        if (data.status.contains("fail", true)) {
            holder.textAdapterAssetDetailStatus.text = "Failure"
            holder.textAdapterAssetDetailStatus.setBackgroundResource(R.drawable.assets_detail_status_fail)

        } else if (data.status.contains("succe", true)) {
            holder.textAdapterAssetDetailStatus.text = "Successful"
            holder.textAdapterAssetDetailStatus.setBackgroundResource(R.drawable.assets_detail_status_success)

        } else if (data.status.contains("process", true)) {
            holder.textAdapterAssetDetailStatus.text = "Processing"
            holder.textAdapterAssetDetailStatus.setBackgroundResource(R.drawable.assets_detail_status_processing)

        } else if (data.status.contains("create", true)) {
            holder.textAdapterAssetDetailStatus.text = "Processing"
            holder.textAdapterAssetDetailStatus.setBackgroundResource(R.drawable.assets_detail_status_processing)
        }

        if (data.inOrOut.equals("OUT", true)) {
            holder.textAdapterAssetDetailAmount.setTextColor(
                ContextCompat.getColor(
                    holder.textAdapterAssetDetailAmount.context,
                    R.color.main_red
                )
            )

            holder.imageAdapterAssetDetail.setImageResource(R.drawable.icon_transfer_out)

            holder.textAdapterAssetDetailAmount.text = "-" + data.amount.toFinanceString()

        } else if (data.inOrOut.equals("IN", true)) {
            holder.textAdapterAssetDetailAmount.setTextColor(
                ContextCompat.getColor(
                    holder.textAdapterAssetDetailAmount.context,
                    R.color.main_blue
                )
            )

            holder.imageAdapterAssetDetail.setImageResource(R.drawable.icon_transfer_in)

            holder.textAdapterAssetDetailAmount.text = "+" + data.amount.toFinanceString()
        }

        holder.rlAdapterAssetDetailContent.setOnClickListener {
            mItemClickListener.invoke(data)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    /** 设置点击事件 */
    fun setItemClickListener(listener: (RevDataGetMyTransactions) -> Unit) {
        mItemClickListener = listener
    }
}